package com.lancer.dynamic.thread.pool.controller;

import com.lancer.dynamic.thread.pool.ThreadPoolMonitor;
import com.lancer.dynamic.thread.pool.domain.Result;
import com.lancer.dynamic.thread.pool.domain.ThreadPoolInfo;
import com.lancer.dynamic.thread.pool.domain.ThreadPoolListVO;
import com.lancer.dynamic.thread.pool.lancerenum.RejectEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/lancer-thread-pool/monitor")
@Slf4j
public class MonitorController {

    @Autowired
    ThreadPoolMonitor threadPoolMonitor;
    // 获取所有线程池的关键信息
    @GetMapping("/queryList")
    public Result<List<ThreadPoolListVO>> queryList(){
        return threadPoolMonitor.queryList();
    }
    // 查询指定名称的线程池的详细信息
    @GetMapping("/queryDetails")
    public Result<ThreadPoolInfo> queryDetails(@RequestParam(value = "poolName") String poolName){
        return threadPoolMonitor.queryDetails(poolName);
    }
    // 关闭指定名称的线程池
    @GetMapping("/shutdown")
    public Result<String> shutdown(@RequestParam(value = "poolName") String poolName,
                                   @RequestParam(value = "isForceStop",required = false) Boolean isForceStop){
        return threadPoolMonitor.shutdown(poolName,isForceStop);
    }
    // 配置一个新的线程池，传入ThreadPoolInfo
    @PostMapping("/buildOrUpdate")
    public Result<String> buildOrUpdate(@RequestBody ThreadPoolInfo threadPoolInfo){
        return threadPoolMonitor.buildOrUpdate(threadPoolInfo);
    }

    // 获取拒绝策略枚举
    @GetMapping("/getRejectEnum")
    public Result<LinkedHashMap<String,String>> getRejectEnum(){
        LinkedHashMap<String, String> rejectEnumList = RejectEnum.getRejectEnumList();
        log.info("RejectEnum list: " + rejectEnumList);
        return Result.success(rejectEnumList);
    }
}
